package com.hfzy.ihk.web.freeswitchCall.support.rmi;


import com.hfzy.ihk.common.util.string.StringTools;
import com.hfzy.ihk.common.util.string.StringUtil;
import com.hfzy.ihk.facade.freeswitchCall.enums.MainCallTypeEnum;
import com.hfzy.ihk.facade.freeswitchCall.vo.CallHistory;
import com.hfzy.ihk.web.freeswitchCall.context.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * Created by Administrator on 2017/6/28.
 *
 * @author czs
 */
public class RmiPushCallTimeTask implements Runnable{

    Logger logger = LoggerFactory.getLogger(RmiPushCallTimeTask.class);

    private CallHistory callHistory;

    public RmiPushCallTimeTask() {
    }

    public RmiPushCallTimeTask(CallHistory callHistory) {

        this.callHistory = callHistory;
    }


    @Override
    public void run() {

        if(callHistory == null)
            return;

        final String sessionId = callHistory.getSessionId();
        if(callHistory.getPageFrom()==null) {
            logger.info("增加呼叫次数失败，getPageFrom为空！sessionId:{}", sessionId);
            return;
        }
        final String pageFrom=callHistory.getPageFrom().toString();


        String callPhone= null;
        if( callHistory.getMainCallType() == MainCallTypeEnum.MAIN_CALL_PHONE_IS_CUSTOMER.getValue())
            callPhone = callHistory.getMainCallPhone();
        else if(callHistory.getMainCallType() == MainCallTypeEnum.MAIN_CALL_PHONE_IS_SALES.getValue()){
            callPhone = callHistory.getTargetCallPhone();
        }

        if(StringUtil.isEmpty(callPhone)){
            logger.info("增加呼叫次数失败，客户号码为空！sessionId:{}",sessionId);
            return;
        }
        String msg = null;
        Long currentCallTimes=0L;
        try {
            //pageFrom: app盘源(0),app联动call(1),置业盘源(2)，置业联动call(3),电子客簿（4），测试座机页面（5）,app 拨号记录(6), (7)
            // LC:联动Call客,CF:联动Call客继续跟进,PC:盘源详细页
            switch (pageFrom){
                case "2":
                    currentCallTimes= BeanUtils.getCloudCallCacheService().incrCallCountByNum("PC",callPhone);
                    msg = StringUtil.format("增加呼叫次数:sessionId:{0},页面来源:{1},客户号码:{2},当前次数:{3}",
                            sessionId, pageFrom,callPhone,currentCallTimes);

                    break;
                case "3":
                    currentCallTimes=BeanUtils.getCloudCallCacheService().incrCallCountByNum("LC",callPhone);
                    msg = StringUtil.format("增加呼叫次数:sessionId:{0},页面来源:{1},客户号码:{2},当前次数:{3}",
                            sessionId, pageFrom,callPhone,currentCallTimes);
                    break;
                case "4":
                case "7":
                    if(StringTools.isNotempty(callHistory.getCreateUserId())) {
                        logger.debug("增加呼叫次数pageFrom：{}",callHistory.getPageFrom());
                        currentCallTimes = BeanUtils.getCloudCallCacheService().incrEbookCallCountByNumUid(callPhone, Integer.valueOf(callHistory.getCreateUserId()));
                        msg = StringUtil.format("增加呼叫次数:sessionId:{0},页面来源:{1},客户号码:{2},当前次数:{3}",
                                sessionId, pageFrom, callPhone, currentCallTimes);
                    }
                    break;
                default:
                    break;
            }

        } catch (Exception e) {
            logger.error("推送呼叫次数异常：msg:{},callTimes:{}",msg,currentCallTimes,e);
        }
    }
}
